package com.dot.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dot.dao.DwarfBookDao;
import com.dot.dao.DwarfPageDao;
import com.dot.service.DwarfBookService;
import com.dot.vo.DwarfBookVo;
import com.dot.vo.DwarfPageVo;

@Service("dwarfBookService")
@Transactional
public class DwarfBookServiceImpl implements DwarfBookService{

	@Resource(name="dwarfPageDao")
	private DwarfPageDao dwarfPageDao;
	
	@Resource(name="dwarfBookDao")
	private DwarfBookDao dwarfBookDao;
	
	@Transactional(propagation = Propagation.REQUIRED)
	public void saveBook(DwarfBookVo book) throws Exception {
		dwarfBookDao.save(book);
		for(DwarfPageVo page : book.getPages()){
			dwarfPageDao.save(page);
		}
	}
	
	@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
	public List<DwarfBookVo> getBookByParentCode(String pCode) throws Exception{
		return dwarfBookDao.getByParentCode(pCode);
	}
	
	@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
	public List<DwarfPageVo> getPageByBookCode(String bCode) throws Exception{
		return dwarfPageDao.getByBookCode(bCode);
	}
	
	public DwarfBookVo getBookByCode(String bCode) throws Exception{
		return dwarfBookDao.getByCode(bCode);
	}
	
	@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
	public List<DwarfBookVo> getRootBook() throws Exception {
		return dwarfBookDao.getRootBook();
	}

}
